package com.p6spy.engine.spy.appender;

import com.p6spy.engine.logging.Category;

/**
 * SQL batch file logger Private: (?) No Appender that writes a trace of JDBC activity into an SQL
 * batch file that can be later "replayed" using a generic SQL client.
 * <p>
 * modify the spy.properties file to make p6spy use this appender by setting
 * appender=com.p6spy.engine.spy.appender.BatchFileLogger.
 * </p>
 * <p>Here's how it works for me: logfile = spy.sql The appender writes the text of SQL
 * statements as well as commit and rollback commands to the specified file, each on a new line. For
 * prepared statements the effective text resulting from substitution of parameter signs with their
 * values is written. Other event categories are logged as SQL comments ("-- " followed by the
 * category name). Exceptions and text are silently discarded. All SQL statements except the last
 * one will have a delimiter character ';' appended to them. This batch format works well with
 * WinSQL. The format is hardcoded, so if you want to make it configurable you'll have to patch my
 * patch or write your own :)
 */
public class BatchFileLogger extends FileLogger {
    public static final char BATCH_SEPARATOR = ';';
    private boolean endOfStatement = true;

    @Override
    public void logException(Exception e) {
    }

    @Override
    public void logSQL(int connectionId, String now, long elapsed, Category category, String prepared, String sql, String url) {
        if (endOfStatement) {
            getStream().println(BATCH_SEPARATOR);
        }
        if (Category.STATEMENT.equals(category)) {
            String actual = null == sql || 0 == sql.length() ? prepared : sql;
            getStream().print(actual);
            endOfStatement = true;
        } else if (Category.COMMIT.equals(category) || Category.ROLLBACK.equals(category)) {
            getStream().print(category);
            endOfStatement = true;
        } else {
            getStream().println("-- " + category);
            endOfStatement = false;
        }
        getStream().flush();
    }

    @Override
    public void logText(String text) {
    }

}
